import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:search_people/utils/component/selected_tag_view.dart';
import 'package:search_people/utils/component/selection/model.dart';
import 'package:search_people/value/topcolors.dart';

import '../controller.dart';

class SelectionTagWidget extends GetView<SelectionController> {
  const SelectionTagWidget({
    Key? key,
    this.normalFontSize,
    this.selectedFontSize,
    required this.tags,
    this.onTap,
  }) : super(key: key);

  final double? normalFontSize;
  final double? selectedFontSize;
  final List<SelectionModel> tags;
  final Function(List<int> idx, List<SelectionModel> md)? onTap;

  @override
  Widget build(BuildContext context) {
    var state = controller.state;
    List<SelectionModel> showTags = [];
    if (tags.length > state.pageSize) {
      showTags.addAll(tags.sublist(0, state.pageSize * (state.pageIndex + 1)));
    } else {
      showTags = tags;
    }

    List<int> initIndexs = [];
    for (var i = 0; i < showTags.length; i++) {
      var m = showTags[i];
      if (m.isSelected) {
        initIndexs.add(i);
      }
    }

    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(10.r),
      child: SmartRefresher(
        onRefresh: () {
          state.rc?.refreshCompleted();
          controller.update();
        },
        onLoading: () {
          state.pageIndex++;
          int count = (state.pageIndex + 1) * state.pageSize;
          if (count < tags.length) {
            if (state.rc?.isLoading == true) {
              state.rc?.loadComplete();
            }
          } else {
            if (state.rc?.isLoading == true) {
              state.rc?.loadNoData();
            }
          }
        },
        controller: state.rc!,
        enablePullUp: true,
        child: SingleChildScrollView(
          child: SelectTagView(
            initIndexs: initIndexs,
            items: showTags.map((e) => e.title).toList(),
            padding: EdgeInsets.symmetric(horizontal: 8.r, vertical: 12.r),
            spacing: 12.r,
            runSpacing: 15.r,
            textStyle: TextStyle(
                color: TopColors.black_33, fontSize: normalFontSize ?? 9.sp),
            selectedTextStyle: TextStyle(
                color: TopColors.main_color,
                fontSize: selectedFontSize ?? 10.sp),
            selectedTagBgColor: TopColors.blueBg,
            isSingleSelect: false,
            onTap: (p0) {
              if (onTap != null) {
                for (var i = 0; i < showTags.length; i++) {
                  var e = showTags[i];
                  e.isSelected = p0.contains(i);
                }

                onTap!(p0,
                    showTags.where((element) => element.isSelected).toList());
              }
            },
          ),
        ),
      ),
    );
  }
}
